﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ecommerceSample
{
    public partial class SearchPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Session["sea"]!=null)
            {
                string aux = Session["sea"].ToString();
                txBSimpleSearch.Text = aux;
                pnlSimpleResult.Visible = true;
                pnlSimpleSearch.Visible = false;
            }
            else
            {
                txBSimpleSearch.Text = "";
                pnlSimpleResult.Visible = false;
                pnlSimpleSearch.Visible = true;
            }
        }

        protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
        {
            MuVSearch.ActiveViewIndex = Int32.Parse(Menu1.SelectedValue);
        }

        protected void btnSimpleSearch_Click(object sender, EventArgs e)
        {
            pnlSimpleResult.Visible = true;
            pnlSimpleSearch.Visible = false;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            pnlSimpleResult.Visible = false;
            pnlSimpleSearch.Visible = true;
        }

        public string MarcarPalabra(string strParrafo)
        {


            if (txBSimpleSearch.Text != "")
            {
                int pos = strParrafo.IndexOf(txBSimpleSearch.Text, 0, strParrafo.Length, StringComparison.OrdinalIgnoreCase);
                return (strParrafo.Substring(0, pos) + "<b style='color: #0099FF'>" + strParrafo.Substring(pos, txBSimpleSearch.Text.Length) + "</b>" + strParrafo.Substring(pos + txBSimpleSearch.Text.Length));

            }
            else
            {
                return (strParrafo);
            }
        }

        public string MarcarTitulo(string strParrafo)
        {

            if (txbAdvancedSearch.Text!="" && ddlSets1.SelectedItem.Text == "Titulo")
            {

                int pos = strParrafo.IndexOf(txbAdvancedSearch.Text, 0, strParrafo.Length, StringComparison.OrdinalIgnoreCase);
                return (strParrafo.Substring(0, pos) + "<b style='color: #0099FF'>" + strParrafo.Substring(pos, txbAdvancedSearch.Text.Length) + "</b>" + strParrafo.Substring(pos + txbAdvancedSearch.Text.Length)); ;
                
            }
            else
            {
                return (strParrafo);
            }
           
        }

        public string MarcarAutor(string strParrafo)
        {

            if (txbAdvancedSearch.Text != "" && ddlSets1.SelectedItem.Text == "Autor")
            {

                int pos = strParrafo.IndexOf(txbAdvancedSearch.Text, 0, strParrafo.Length, StringComparison.OrdinalIgnoreCase);
                return (strParrafo.Substring(0, pos) + "<b style='color: #0099FF'>" + strParrafo.Substring(pos, txbAdvancedSearch.Text.Length) + "</b>" + strParrafo.Substring(pos + txbAdvancedSearch.Text.Length)); ;

            }
            else
            {
                return (strParrafo);
            }

        }

        public string MarcarContenido(string strParrafo)
        {

            if (txbAdvancedSearch.Text != "" && ddlSets1.SelectedItem.Text == "Contenido")
            {

                int pos = strParrafo.IndexOf(txbAdvancedSearch.Text, 0, strParrafo.Length, StringComparison.OrdinalIgnoreCase);
                int i = 50;
                int aux = 0;
                while (i >= 0)
                {
                    if (pos - i >= 0)
                    {
                        aux = i;
                        i = -1;
                    }
                    else
                    {
                        i = i - 1;
                    }
                }
                int theEnd = pos + txbAdvancedSearch.Text.Length;
                if (aux > 10)
                {
                    return ("..." + strParrafo.Substring(pos - aux + 10, pos) + "<b style='color: #0099FF'>" +
                        strParrafo.Substring(pos, txbAdvancedSearch.Text.Length) + "</b>" + strParrafo.Substring(theEnd, theEnd + 50 - aux) + "...");
                }
                else
                {
                    return (strParrafo.Substring(pos - aux, pos) + "<b style='color: #0099FF'>" +
                        strParrafo.Substring(pos, txbAdvancedSearch.Text.Length) + "</b>" + strParrafo.Substring(theEnd, theEnd + 50 - aux) + "...");
                }
            }
            else
            {
                return (LimitWords(strParrafo, 50));
            }
        }
        
        public String LimitWords(String Input, int Limit)
        {
            // split input string into words (max 21...last words go in last element)
            String[] Words = Input.Split(new char[] { ' ' }, Limit);

            // if we reach maximum words, replace last words with elipse
            if (Words.Length == Limit)
                Words[Limit - 1] = "...";
            else
                return Input;  // nothing to do

            // build new output string
            String Output = String.Join(" ", Words);
            return Output;
        }


        protected void btnAdvancedSearch_Click(object sender, EventArgs e)
        {
            if (ddlSets1.SelectedItem.Text == "Titulo")
            {
                gvAdvancedSearchTitle.DataSource = SqlTitle;
                gvAdvancedSearchTitle.DataBind();
                pnlAdvancedTitleResult.Visible = true;
            }
            else if (ddlSets1.SelectedItem.Text == "Autor")
            {
                gvAdvancedSearchTitle.DataSource = SqlAutor;
                gvAdvancedSearchTitle.DataBind();
                pnlAdvancedTitleResult.Visible = true;
            }
            else if (ddlSets1.SelectedItem.Text == "Contenido")
            {
                gvAdvancedSearchTitle.DataSource = SqlContent;
                gvAdvancedSearchTitle.DataBind();
                pnlAdvancedTitleResult.Visible = true;
            }
        }
    }
}